package org.krishna.service;

import org.krishna.Dao.SaveData;
import org.krishna.model.Employee;
import org.krishna.model.JobStatus;
import org.krishna.model.JobTitle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

//@Service("dataTransferHandler")
@Service("serviceHandler")
@Transactional(propagation = Propagation.REQUIRED)
public class ServiceHandlerImpl implements ServiceHandler {
	
	private static final Logger logger = LoggerFactory.getLogger(ServiceHandlerImpl.class);

	@Autowired
	@Qualifier("saveData")
	SaveData saveData;

	@Override
	@Transactional
	public void addEmployee(String id, String firstName, String lastName,
			String middleName, String gender, String maritalStatus,
			String jobTitleId, String jobStatusId) {
		int jobId = Integer.parseInt(jobTitleId);
		int statusId = Integer.parseInt(jobStatusId);
		JobTitle jobTitle = saveData.getJobTitleById(jobId);
		JobStatus jobStatus = saveData.getJobStatusById(statusId);
		if(null!= jobTitle && null!= jobStatus){
			logger.info("hello");
			Employee employeeById = saveData.getEmployeeById(id);
			if(null==employeeById){
				Employee employee = new Employee();
				employee.setEmployeeId(id);
				employee.setEmpFirstName(firstName);
				employee.setEmpLastName(lastName);
				employee.setEmpMiddleName(middleName);
				employee.setEmpGender(Integer.parseInt(gender));
				employee.setEmpMaritalStatus(maritalStatus);
				employee.setJobtitle(jobTitle);
				employee.setJobStatus(jobStatus);
				boolean saveEmployeeDetails = saveData.saveEmployeeDetails(employee);
				logger.info("Saved Employee Status is "+saveEmployeeDetails);
			}else{
				logger.info("Employee with Id "+id+ " already exists");
				
			}
		}
		
	}

	

	

}
